﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class controls_usrPhoneNo : System.Web.UI.UserControl
{
    protected string strPhoneNo = "";
    protected string strValidatinoGroup = "";
    protected string strPhoneNoErrorMessage = "Phone no must be 8 digits number value.";
    protected string strPhoneNoValidErrorMessage = "Phone number is not valid.";
    protected string strPhoneNoRequireErrrorMessage = "Require phone number.";
    protected bool bRequire = false;
    protected bool bEnabled = true;
    protected bool bReadOnly = false;
    protected string strCssClass = "";
    protected string strFontSize = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            txtPhoneNo.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtPhoneNo.ClientID + "','" + rfvPhoneNo.ClientID + "','" + csvPhoneNo.ClientID + "');DefaultMobile(this, event);");
            txtPhoneNo.Attributes.Add("onfocus", "ClearText(this, event);");

            if (rfvPhoneNo.Enabled)
            {
                
            }
            
        }
    }

    public void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        if (rfvPhoneNo.Enabled && rfvPhoneNo.ValidationGroup == strValidationGroup)
        {
            rfvPhoneNo.Validate();
            if (rfvPhoneNo.IsValid == false)
            {
                txtPhoneNo.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtPhoneNo.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtPhoneNo.ControlStyle.CssClass = "";
        }

        if (revPhoneNo.Enabled && revPhoneNo.ValidationGroup == strValidationGroup)
        {
            if (rfvPhoneNo.IsValid)
            {
                revPhoneNo.Validate();
                if (revPhoneNo.IsValid == false)
                {
                    txtPhoneNo.ControlStyle.CssClass = "validationError";
                    revPhoneNo.ErrorMessage = strPhoneNoErrorMessage;
                    revPhoneNo.Text = "*";
                }
                else
                {
                    txtPhoneNo.ControlStyle.CssClass = "";
                }
            }
        }
        else
        {
            txtPhoneNo.ControlStyle.CssClass = "";
        }

        if (csvPhoneNo.Enabled && csvPhoneNo.ValidationGroup == strValidationGroup)
        {
            if (rfvPhoneNo.IsValid && revPhoneNo.IsValid)
            {
                csvPhoneNo.Validate();
                if (csvPhoneNo.IsValid == false)
                {
                    txtPhoneNo.ControlStyle.CssClass = "validationError";
                    csvPhoneNo.ErrorMessage = strPhoneNoValidErrorMessage;
                    csvPhoneNo.Text = "*";
                }
                else
                {
                    txtPhoneNo.ControlStyle.CssClass = "";
                }
            }
        }
        else
        {
            txtPhoneNo.ControlStyle.CssClass = "";
        }


    }

    protected void CheckPhoneNoServerValidation(object source, ServerValidateEventArgs args)
    {
        try
        {
            // Test whether the lenght of value is < 8 or Is Number Value.
            if (args.Value.Length < 8)
            {
                if (args.Value.Length == 0)
                    args.IsValid = true;
                else
                    args.IsValid = false;
            }
            else
            {
                int i = int.Parse(args.Value);
                if (i > 0)
                {
                    string chrStartPhoneNo = args.Value.Substring(0, 1);
                    Int16 intStartPhoneNo = Convert.ToInt16(chrStartPhoneNo);

                    if (intStartPhoneNo != 8 &&
                        intStartPhoneNo != 9 &&
                        intStartPhoneNo != 6)
                    {
                        args.IsValid = false;
                    }
                    else
                    {
                        args.IsValid = true;
                    }
                }
                else
                {
                    args.IsValid = false;
                }
            }
        }
        catch (Exception ex)
        {
            args.IsValid = false;
        }
    }

    public bool IsEnabled
    {
        get { return bEnabled; }
        set
        {
            bEnabled = value;
            SetEnabled();
        }
    }

    public bool IsReadOnly
    {
        get { return bReadOnly; }
        set
        {
            bReadOnly = value;
            SetReadOnly();
        }
    }

    public bool IsRequired
    {
        get { return bRequire; }
        set
        {
            bRequire = value;
            rfvPhoneNo.Enabled = bRequire;
        }
    }

    public void IsEnableValidation(bool bValue)
    {
        rfvPhoneNo.Enabled = bValue;
        bRequire = bValue;
    }

    public string PhoneNo
    {
        get { return strPhoneNo; }
        set { strPhoneNo = value; }
    }

    public string CssClass
    {
        get { return strCssClass; }
        set { strCssClass = value; }
    }

    public string FontSize
    {
        get { return strFontSize; }
        set { strFontSize = value; }
    }

    public string ValidationGroup
    {
        get { return strValidatinoGroup; }
        set { strValidatinoGroup = value; }
    }

    public string PhoneNoErrorMessage
    {
        get { return strPhoneNoErrorMessage; }
        set { strPhoneNoErrorMessage = value; }
    }

    public string PhoneNoValidErrorMessage
    {
        get { return strPhoneNoValidErrorMessage; }
        set { strPhoneNoValidErrorMessage = value; }
    }

    public string PhoneNoRequireErrorMessage
    {
        get { return strPhoneNoRequireErrrorMessage; }
        set { strPhoneNoRequireErrrorMessage = value; }
    }

    private void SetEnabled()
    {
        //ddlGender.SelectedIndex = 0;
        txtPhoneNo.Enabled = bEnabled;
        csvPhoneNo.Enabled = bEnabled;
        rfvPhoneNo.Enabled = bEnabled;
        revPhoneNo.Enabled = bEnabled;
    }

    private void SetReadOnly()
    {
        txtPhoneNo.IsReadOnly = bReadOnly;
    }

    protected override void OnPreRender(EventArgs e)
    {
        //SetEnabled();

        if (bRequire && bEnabled)
            rfvPhoneNo.Enabled = true;
        else
            rfvPhoneNo.Enabled = false;

        if (strValidatinoGroup != null)
        {
            if (strValidatinoGroup != string.Empty)
            {
                csvPhoneNo.ValidationGroup = strValidatinoGroup;
                rfvPhoneNo.ValidationGroup = strValidatinoGroup;
                revPhoneNo.ValidationGroup = strValidatinoGroup;
            }
        }

        if (strPhoneNo != string.Empty)
        {
            txtPhoneNo.Text = strPhoneNo;
        }

        if (strCssClass != string.Empty)
        {
            txtPhoneNo.CssClass = strCssClass;
        }

        if (strFontSize != string.Empty)
        {
            txtPhoneNo.Font.Size = new FontUnit(Convert.ToDouble(strFontSize), UnitType.Pixel);
        }

        //if (strPhoneNoErrorMessage != string.Empty)
        //{
        //    csvPhoneNo.ErrorMessage = strPhoneNoValidErrorMessage;
        //}

        //if (strPhoneNoValidErrorMessage != string.Empty)
        //{
        //    revPhoneNo.ErrorMessage = strPhoneNoErrorMessage;
        //}

        //if (strPhoneNoRequireErrrorMessage != string.Empty)
        //{
        //    rfvPhoneNo.ErrorMessage = strPhoneNoRequireErrrorMessage;
        //}
    }

    public void SetProperty()
    {
        strPhoneNo = txtPhoneNo.Text;
    }

    public string GetPhoneNo()
    {
        strPhoneNo = txtPhoneNo.Text;
        string _idno = strPhoneNo;

        return _idno;
    }
}
